/**
 * @file wa_font.c
 * 
 */

/*********************
 *      INCLUDES
 *********************/
#include "wa_font.h"


/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/

/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

/**
 * get bitmap font of letter
 * @param font_p point of font
 * @param letter letter's ascii
 * @return bitmap font of letter
 */
uint8_t* wa_font_get_bitmap(wa_font* font_p, uint32_t letter)
{
	if (font_p->type == WA_FONT_TYPE_INSIDE)
	{
		wa_font_str* font_str;
		font_str = font_p->first_page;
		while (font_str != NULL)
		{
			if ((letter >= font_str->first_ascii) && (letter <= font_str->last_ascii))
			{
				uint32_t index = letter - font_str->first_ascii;
				return &(font_str->bitmap[font_str->map[index]]);
			}
			font_str = font_str->next_page;
		}
		return NULL;
	}
	else if (font_p->type == WA_FONT_TYPE_OUTSIDE)
	{
		if (font_p->get_bitmap_from_outside != NULL)
		{
			return font_p->get_bitmap_from_outside(font_p, letter);
		}
		return NULL;
	}
	return NULL;
}

/**
 * get letter bitmap width
 * @param font_p point of font
 * @param letter letter's ascii
 * @return letter bitmap width
 */
uint8_t wa_font_get_width(wa_font* font_p, uint32_t letter)
{
	if (font_p->type == WA_FONT_TYPE_INSIDE)
	{
		wa_font_str* font_str;
		font_str = font_p->first_page;
		while (font_str != NULL)
		{
			if ((letter >= font_str->first_ascii) && (letter <= font_str->last_ascii))
			{
				uint32_t index = letter - font_str->first_ascii;
				return font_str->width[index];
			}
			font_str = font_str->next_page;
		}
		return 0;
	}
	else if (font_p->type == WA_FONT_TYPE_OUTSIDE)
	{
		if (font_p->get_width_from_outside != NULL)
		{
			return font_p->get_width_from_outside(font_p, letter);
		}
		return 0;
	}
	return 0;
}

/**
 * get font height
 * @param font_p point of font
 * @return font height
 */
uint8_t wa_font_get_height(wa_font* font_p)
{
	return font_p->height_row;
}

/**
 * get width and height of display str
 * @param width point of width
 * @param height point of height
 * @param str string
 * @param font_p point of font
 * @param row_space 
 * @param col_space 
 */
void wa_font_get_str_size(int* width, int* height, uint8_t* str, wa_font* font_p, uint8_t row_space, uint8_t col_space)
{
	uint8_t hz=0;
	int letter_w, letter_h;
	int width_max=0;
	int height_max=0;
	int col_width=0;
	letter_h=font_p->height_row;
	if(str==NULL)
	{
		return;
	}
	while(*str!='\0')
	{
		if(hz==0)
		{
			if(*str>0x80)
			{
				hz=1;
			}
			else
			{
				if((*str=='\r')||(*str=='\n'))
				{
					height_max+=letter_h;
					height_max+=row_space;
					width_max=WA_MATH_MAX(width_max, col_width);
					str++;
					col_width=0;
				}
				else
				{
					letter_w=wa_font_get_width(font_p,(uint32_t)(*str));
					col_width+=letter_w;
					if((*(str+1)!='\r')&&(*(str+1)!='\n'))
					{
						col_width+=col_space;
					}
					str++;
				}
				
			}
		}
		else
		{
			uint32_t letter;
			hz=0;
			letter=(((uint32_t)(*str))<<8)+((uint32_t)(*(str+1)));
			letter_w=wa_font_get_width(font_p,letter);
			col_width+=letter_w;
			col_width+=col_space;
			str+=2;
		}
	}
	width_max=WA_MATH_MAX(width_max, col_width);
	height_max+=letter_h;
	*width=width_max;
	*height=height_max;
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
